package com.wangshb.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshb.upms.core.common.BeanMapper;
import com.wangshb.upms.core.common.PageRequest;
import com.wangshb.upms.dto.PageResponseDto;
import com.wangshb.upms.dto.upms.role.*;
import com.wangshb.upms.entity.UpmsRole;
import com.wangshb.upms.service.UpmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author auto code
 */
@RestController
public class UpmsRoleController {

    @Autowired
    private UpmsRoleService upmsRoleService;


    @PostMapping("/upms/role/listPage")
    public PageResponseDto listPage(@RequestBody PageRequest<UpmsRoleQueryRequestDto> page) {
        Page<UpmsRole> resultPage = this.upmsRoleService.findPage(page.getPage(), page.getLimit(), BeanMapper.map(page.getCondition(), UpmsRole.class));
        return PageResponseDto.builder().total(resultPage.getTotal()).data(BeanMapper.mapAsList(resultPage.getRecords(), UpmsRoleQueryResponseDto.class)).build();
    }


    @PostMapping("/upms/role/add")
    public Long addUpmsRole(@RequestBody UpmsRoleAddRequestDto addDto) {
        return this.upmsRoleService.addUpmsRole(BeanMapper.map(addDto, UpmsRole.class));
    }


    @PostMapping("/upms/role/edit")
    public void editUpmsRole(@RequestBody UpmsRoleEditRequestDto editDto) {
        this.upmsRoleService.updateUpmsRoleWithLock(BeanMapper.map(editDto, UpmsRole.class));
    }


    @PostMapping("/upms/role/del")
    public void delUpmsRole(@RequestBody UpmsRoleDelRequestDto delDto) {
        this.upmsRoleService.delUpmsRole(BeanMapper.map(delDto, UpmsRole.class));
    }


    @PostMapping("/upms/role/get")
    public UpmsRoleGetResponseDto findById(@RequestBody UpmsRoleGetRequestDto getDto) {
        QueryWrapper<UpmsRole> upmsRoleQueryWrapper = new QueryWrapper<>();
        upmsRoleQueryWrapper.setEntity(BeanMapper.map(getDto, UpmsRole.class));
        UpmsRole upmsRole = this.upmsRoleService.getOne(upmsRoleQueryWrapper);
        return null == upmsRole ? null : BeanMapper.map(upmsRole, UpmsRoleGetResponseDto.class);
    }
}
